package net.shao.activity;

import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.CallSuper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.hyphenate.EMCallBack;
import com.hyphenate.EMError;
import com.hyphenate.chat.EMChatManager;
import com.hyphenate.chat.EMClient;
import com.socks.library.KLog;

import net.shao.R;
import net.shao.base.BaseActivity;
import net.shao.utils.ToastUtils;
import net.shao.utils.ViewUtils;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.lang.ref.WeakReference;

public class LoginActivity extends BaseActivity {

    @ViewInject(R.id.et_tel_LogReg)
    private EditText et_tel_LogReg;
    @ViewInject(R.id.et_password_LogReg)
    private EditText et_password_LogReg;
    @ViewInject(R.id.btn_UserloginReg_logreg)
    private Button btn_UserLogin;
    @ViewInject(R.id.tv_toRegistered)
    private TextView tv_toRegistered;

    EMLoginHandle handle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        x.view().inject(this);

        handle = new EMLoginHandle(new WeakReference<>(this));

        btn_UserLogin.setOnClickListener(v -> {
            EMClient.getInstance().login(et_tel_LogReg.getText().toString(), et_password_LogReg.getText().toString(),
                    new EMCallBack() {
                        @Override
                        public void onSuccess() {
                            KLog.i("登录成功");
                            Message message = new Message();
                            message.what = EMError.EM_NO_ERROR;
                            handle.handleMessage(message);
                        }

                        @Override
                        public void onError(int i, String s) {
                            KLog.i("出现异常情况>>>>" + i + ",信息>>>" + s);
                            Message message = new Message();
                            message.what = i;
                            handle.handleMessage(message);
                        }

                        @Override
                        public void onProgress(int i, String s) {
                            KLog.i("等待>>>>" + i + ",信息>>>" + s);
                        }
                    });
        });

        tv_toRegistered.setOnClickListener(v -> {
            toActivity(RegisterActivity.class);
        });
    }


    static class EMLoginHandle extends Handler {

        WeakReference<LoginActivity> weakReference;


        public EMLoginHandle(WeakReference<LoginActivity> weakReference) {
            this.weakReference = weakReference;
        }

        @Override
        public void handleMessage(Message msg) {
            LoginActivity activity = weakReference.get();

            int emStatus = msg.what;
            Looper.prepare();
            switch (emStatus) {
                case EMError.EM_NO_ERROR:
                    EMClient.getInstance().groupManager().loadAllGroups();
                    EMClient.getInstance().chatManager().loadAllConversations();
                    ToastUtils.showToast(activity, "登录成功");
                    ViewUtils.toActivity(activity,ChatListActivity.class);
                    break;
                case EMError.SERVER_UNKNOWN_ERROR:
                    ToastUtils.showToast(activity, "出现未知异常，请检查网络是否打开");
                    break;
                case EMError.USER_NOT_FOUND:
                    ToastUtils.showToast(activity, "您还未注册");
                    break;
            }
            Looper.loop();
        }
    }

}
